from rest_framework.viewsets import ModelViewSet
from django.utils import timezone
import pytz
from django.conf import settings

from meiduo_admin.serializers.orderSerializer import OrderSerializer
from payment.models import OrderInfo
from meiduo_admin.utils import PaginatorShow
from meiduo_admin.permissions import *


class OrderView(ModelViewSet):
    queryset = OrderInfo.objects.all().order_by("pk")
    serializer_class = OrderSerializer
    permission_classes = [IsAdminUser]

    pagination_class = PaginatorShow

    def get_queryset(self):

        local_timezone = pytz.timezone(settings.TIME_ZONE)

        keyword = self.request.query_params.get("keyword")
        if keyword:
            queryset = self.queryset.filter(order_id__contains=keyword)
        else:
            queryset = self.queryset.all()

        for instance in queryset:
            create_time = instance.create_time.astimezone(local_timezone)
            instance.create_time = create_time
        return queryset
